let router = require('../router/Router');
let noticeService = require('../service/noticeService');
const { BusiError, SysError } = require('../exception/exception');
let userService = require('../service/userService');
var     P = require("bluebird");
let roomCacheService = require('../service/RoomCacheService');
let gameService = require('../service/gameService');
let redis = require('../dao/redis');
let redisConst = require('../const/redisConst');
/**
 * uid, page
 */
var   onWork=0;
var   cnt=0;
var   timerTick=-1;
router.onTimer = async function () {
    var  debug=0;
    debug++;
    if(onWork==1)
    {
        return;
    }
    onWork=1;
    //console.logg("ontimer ")
    var  t=(new Date().getTime()-timerTick)/1000/60/60;
    if(t>=5)
    {
        timerTick=new Date().getTime()
        console.logg("onTimer ")
    }
    cnt++;
    var   res=await  redis.scanMap(redisConst.gamingMap,0,2000)
    var  count=Number(res[0]);
    var  list=res[1];
    if(list.length==0){
        onWork=0;
        return;
    }

    var   len=list.length/2;
    for(var n=0;n<len;n++) {
        var roomId = list[n * 2];
        var tick = list[n * 2 + 1]
        try{

            var   time=(new Date().getTime()-tick)/1000/60;
            if(time>=5)
            {

                var  roomInfo=await roomCacheService.getRoom(roomId)
                if(roomInfo)
                {
                    console.logg("5分钟没有活跃玩家在房间里需要解散房间",roomId,roomInfo)
                    await gameService.returnCardByNotify(roomInfo,roomId)
                    await roomCacheService.removeRoom(roomId)
                }
                await  redis.delHashItem(redisConst.gamingMap,roomId);
            }
        }catch(e)
        {

        }

    }

    //await P.delay(10000)
    //console.logg("ontimer End ",cnt)
    onWork=0;


};
